package com.bsh.controller.front;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import org.springframework.web.servlet.ModelAndView;

import com.bsh.mybatis.model.IndexImage;
import com.bsh.service.IndexImageService;
import com.bsh.utils.PageJosnMessage;
import com.bsh.utils.PageTools;
import com.bsh.utils.Pager;
import com.bsh.utils.UploadUtils;
import com.mysql.jdbc.StringUtils;

/**
 * 邀请返利
 * 
 * @author Administrator
 *
 */
@Controller
public class IndexImageController {
	private static Logger log = LoggerFactory.getLogger(IndexImageController.class);

	@Autowired
	HttpServletRequest request;
	@Autowired
	HttpServletResponse response;
	@Autowired
	private IndexImageService indexImgServ;
	@Autowired
	CommonsMultipartResolver multipartResolver;

	/**
	 * 首页轮播图列表
	 */
	@RequestMapping(value = "/front/banner/list")
	public ModelAndView bannerList() {
		String title = request.getParameter("title") == null ? null : request.getParameter("title").toString();

		ModelAndView mv = new ModelAndView();
		mv.setViewName("/pages/front/indexImageList");

		String pageStr = request.getParameter("pageIndex");
		Integer pageIndex = Integer.parseInt(StringUtils.isNullOrEmpty(pageStr) ? "1" : pageStr);

		String sizeStr = request.getParameter("pageSize");
		Integer pageSize = Integer.parseInt(StringUtils.isNullOrEmpty(sizeStr) ? "10" : sizeStr);

		Pager pager = new Pager(pageIndex, pageSize);
		List<Map<String, Object>> list = indexImgServ.getPage(pager, title);
		mv.addObject("list", list);
		mv.addObject("pager", pager);
		mv.addObject("title", title);
		mv.addObject("type", "index");
		return mv;
	}
	
	/**
	 * 商户首页轮播图列表
	 */
	@RequestMapping(value = "/front/banner/list/merchant")
	public ModelAndView merchantBannerList() {
		String title = request.getParameter("title") == null ? null : request.getParameter("title").toString();

		ModelAndView mv = new ModelAndView();
		mv.setViewName("/pages/front/indexImageList");

		String pageStr = request.getParameter("pageIndex");
		Integer pageIndex = Integer.parseInt(StringUtils.isNullOrEmpty(pageStr) ? "1" : pageStr);

		String sizeStr = request.getParameter("pageSize");
		Integer pageSize = Integer.parseInt(StringUtils.isNullOrEmpty(sizeStr) ? "10" : sizeStr);

		Pager pager = new Pager(pageIndex, pageSize);
		List<Map<String, Object>> list = indexImgServ.getMerchantPage(pager, title);
		mv.addObject("list", list);
		mv.addObject("pager", pager);
		mv.addObject("title", title);
		mv.addObject("type", "merchant");
		return mv;
	}

	/**
	 * 修改首页banner--初使化
	 */
	@RequestMapping(value = "/front/banner/edit/{id}")
	public ModelAndView edit(@PathVariable(value = "id") Integer id) {
		ModelAndView mv = new ModelAndView();
		mv.setViewName("/pages/front/indexImageEdit");
		IndexImage banner = indexImgServ.getIndexImage(id);
		mv.addObject("banner", banner);
		return mv;
	}

	/**
	 * 修改首页banner--保存
	 * 
	 * @throws IOException
	 * @throws IllegalStateException
	 */
	@RequestMapping(value = "/front/banner/edit/save")
	public ModelAndView editSave(@RequestParam(value = "upimg") MultipartFile file,
			@ModelAttribute IndexImage banner) throws IllegalStateException, IOException {		
		String fileUrl= "";
		if (!file.isEmpty()) {
			fileUrl = UploadUtils.save(file, "/upload/public/banner");
			banner.setImg(fileUrl);
		}		
		Integer res = indexImgServ.editIndexImage(banner,!file.isEmpty());
		
		ModelAndView mv = new ModelAndView();
		mv.setViewName("/pages/front/indexImageEdit");
		mv.addObject("banner", banner);

		String msg = PageTools.alertCloseFraRefresh("修改首页banner", "保存成功！", 1000);
		mv.addObject("actionMsg", msg);
		return mv;
	}

	/**
	 * 添加首页banner--初使化
	 */
	@RequestMapping(value = "/front/banner/add/{type}")
	public ModelAndView add(@PathVariable(value="type")String type) {
		ModelAndView mv = new ModelAndView();
		mv.setViewName("/pages/front/indexImageAdd");
		mv.addObject("type", type);
		return mv;
	}

	/**
	 * 添加首页banner--保存
	 */
	@RequestMapping(value = "/front/banner/add/save")
	public ModelAndView addSave(@RequestParam(value = "upimg") MultipartFile file,
			@ModelAttribute IndexImage banner) throws IllegalStateException, IOException {		
		String fileUrl= "";
		if (!file.isEmpty()) {
			fileUrl = UploadUtils.save(file, "/upload/public/banner");
			banner.setImg(fileUrl);
		}		
		Integer res = indexImgServ.addIndexImage(banner);
		
		ModelAndView mv = new ModelAndView();
		mv.setViewName("/pages/front/indexImageAdd");
		mv.addObject("banner", banner);

		String msg = PageTools.alertCloseFraRefresh("添加首页banner", "保存成功！", 1000);
		mv.addObject("actionMsg", msg);
		return mv;
	}

	/**
	 * 查看首页banner信息
	 */
	@ResponseBody
	@RequestMapping(value = "/front/banner/view/{id}")
	public PageJosnMessage view(@PathVariable(value = "id") Integer id) {
		IndexImage banner = indexImgServ.getIndexImage(id);
		PageJosnMessage msg = new PageJosnMessage();
		msg.putData("banner", banner);
		return msg;
	}

	/**
	 * 启用、禁用首页banner
	 */
	@ResponseBody
	@RequestMapping(value = "/front/banner/enable")
	public PageJosnMessage enable(@RequestParam int id, @RequestParam int isEnable) {
		Integer res = indexImgServ.enable(id, isEnable == 1);
		PageJosnMessage msg = new PageJosnMessage();
		res = (res == null ? 1 : res);
		msg.putData("status", res > 0 ? 1 : 0);
		return msg;
	}

	/**
	 * 批量禁用
	 */
	@ResponseBody
	@RequestMapping(value = "/front/banner/disable/batch")
	public PageJosnMessage disableBatch(@RequestBody int[] idList) {
		Integer res = indexImgServ.disableBatch(idList);
		PageJosnMessage msg = new PageJosnMessage();
		res = (res == null ? 1 : res);
		msg.putData("status", res > 0 ? 1 : 0);
		return msg;
	}

	/**
	 * 批量启用
	 */
	@ResponseBody
	@RequestMapping(value = "/front/banner/enable/batch")
	public PageJosnMessage enableBatch(@RequestBody int[] idList) {
		Integer res = indexImgServ.enableBatch(idList);
		PageJosnMessage msg = new PageJosnMessage();
		res = (res == null ? 1 : res);
		msg.putData("status", res > 0 ? 1 : 0);
		return msg;
	}

	/**
	 * 删除
	 */
	@ResponseBody
	@RequestMapping(value = "/front/banner/delete/{id}")
	public PageJosnMessage delete(@PathVariable(value = "id") Integer id) {
		Integer res = indexImgServ.deleteIndexImage(id);
		PageJosnMessage msg = new PageJosnMessage();
		res = (res == null ? 1 : res);
		msg.putData("status", res > 0 ? 1 : 0);
		return msg;
	}

}
